//
//  HomeStatusCell.swift
//  LCYWeiBo
//
//  Created by 李长友 on 16/9/6.
//  Copyright © 2016年 app. All rights reserved.
//

import UIKit

//图片之间的距离
private let margin : CGFloat = 5.0
//图片宽度
let pictureWidth = (SCREEN_WIDTH - 30.0 - 2.0 * margin) / 3.0
//图片高度
let pictureHeight = pictureWidth

class HomeStatusCell: UITableViewCell {

    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var verifiedView: UIImageView!
    @IBOutlet weak var screenNameLabel: UILabel!
    @IBOutlet weak var vipView: UIImageView!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var sourceLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var reContentLabel: UILabel!
    @IBOutlet weak var repostButton: UIButton!
    @IBOutlet weak var commentButton: UIButton!
    @IBOutlet weak var attitudeButton: UIButton!
    @IBOutlet weak var pictureView: PictureCollectionView!
    @IBOutlet weak var reBackgroundView: UIView!
    @IBOutlet weak var pictureViewWidth: NSLayoutConstraint!
    @IBOutlet weak var pictureViewHeight: NSLayoutConstraint!
    @IBOutlet weak var contentBottomCon: NSLayoutConstraint!
    
    @IBOutlet weak var reContentBottomCon: NSLayoutConstraint!
    var status: StatusModel? {
        didSet {
            
            guard let status = status else {
                return
            }
            
            //头像
            iconView.sd_setImageWithURL(NSURL(string: status.user.avatar_large ?? ""), placeholderImage: UIImage(named: "avatar_default_small"))
            //认证
            verifiedView.image = status.user.verified_image
            //昵称
            screenNameLabel.text = status.user.screen_name
            //会员
            vipView.image = status.user.vip_image
            //昵称颜色
            screenNameLabel.textColor = vipView.image == nil ? UIColor.blackColor() : UIColor.orangeColor()
            //时间
            timeLabel.text = status.show_created_at
            //来源
            if let show_source = status.show_source {
                sourceLabel.text = "来自" + show_source
            }
            //正文
            contentLabel.text = status.text
            //转发正文
            if let retweeted_status = status.retweeted_status {
                if let name = status.retweeted_status?.user.screen_name {
                    reContentLabel.text = "@\(name):" + (retweeted_status.text ?? "")
                }
                //转发配图
                pictureView.dataArray = (status.retweeted_status?.show_pic_urls)!
                reBackgroundView.hidden = false
                contentBottomCon.constant = 20
            } else {
                reContentLabel.text = ""
                //配图
                pictureView.dataArray = status.show_pic_urls
                reBackgroundView.hidden = true
                contentBottomCon.constant = 0
            }
            
            if pictureView.dataArray.count == 0 {
                reContentBottomCon.constant = 0
            } else {
                reContentBottomCon.constant = 10
            }
            
            let layout = pictureView.collectionViewLayout as! UICollectionViewFlowLayout
            layout.itemSize = CGSize(width: pictureWidth, height: pictureHeight)
            pictureViewWidth.constant = calculatePictureViewSize(pictureView.dataArray.count).width
            pictureViewHeight.constant = calculatePictureViewSize(pictureView.dataArray.count).height
            //转发
            if status.reposts_count != 0 {
                repostButton.setTitle("\(status.reposts_count)", forState: .Normal)
            } else {
                repostButton.setTitle("转发", forState: .Normal)
            }
            //评论
            if status.comments_count != 0 {
                commentButton.setTitle("\(status.comments_count)", forState: .Normal)
            } else {
                commentButton.setTitle("评论", forState: .Normal)
            }
            //点赞
            if status.attitudes_count != 0 {
                attitudeButton.setTitle("\(status.attitudes_count)", forState: .Normal)
            } else {
                attitudeButton.setTitle("赞", forState: .Normal)
            }
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}

extension HomeStatusCell {
    
    private func calculatePictureViewSize(count: Int) -> CGSize {
        
        switch count {
        case 0:
            return CGSizeZero
        case 2:
            return CGSize(width: pictureWidth * 2 + margin, height:pictureHeight)
        case 4:
            return CGSize(width: pictureWidth * 2.0 + margin, height: pictureHeight * 2.0 + margin)
        default:
            return CGSize(width: SCREEN_WIDTH - 30.0, height: pictureHeight * CGFloat((count - 1) / 3  + 1) + margin * CGFloat((count - 1) / 3))
        }
    }
}













